package com.coment.game.config;

import com.xs.action.exection.ActionException;
import java.util.List;

public class Reward {
   private Integer type;
   private List<Item> items;
   private Item reward;
   String describe;

   public Item getReward() {
      if (this.type != null && this.items != null && !this.items.isEmpty()) {
         if (this.type != 2) {
            throw new ActionException("非随机礼包无法使用");
         } else {
            return this.getRandomRewardByWeight();
         }
      } else {
         throw new ConfigException("礼包配置错误");
      }
   }

   public Item getReward(int selectedIndex) {
      if (this.type == 1 && this.items != null && !this.items.isEmpty()) {
         if (selectedIndex >= 0 && selectedIndex < this.items.size()) {
            this.reward = this.items.get(selectedIndex);
            return this.reward;
         } else {
            return null;
         }
      } else {
         return null;
      }
   }

   private Item getRandomRewardByWeight() {
      if (this.items != null && !this.items.isEmpty()) {
         int totalWeight = this.items.stream().mapToInt((itemx) -> itemx.getWeight() != null ? itemx.getWeight() : 1).sum();
         if (totalWeight <= 0) {
            int randomIndex = (int)(Math.random() * (double)this.items.size());
            this.reward = this.items.get(randomIndex);
            return this.reward;
         } else {
            int randomValue = (int)(Math.random() * (double)totalWeight);
            int currentWeight = 0;

            for(Item item : this.items) {
               int weight = item.getWeight() != null ? item.getWeight() : 1;
               currentWeight += weight;
               if (randomValue < currentWeight) {
                  this.reward = item;
                  return this.reward;
               }
            }

            this.reward = this.items.getLast();
            return this.reward;
         }
      } else {
         return null;
      }
   }

   
   public Integer getType() {
      return this.type;
   }

   
   public List<Item> getItems() {
      return this.items;
   }

   
   public String getDescribe() {
      return this.describe;
   }

   
   public void setType(final Integer type) {
      this.type = type;
   }

   
   public void setItems(final List<Item> items) {
      this.items = items;
   }

   
   public void setReward(final Item reward) {
      this.reward = reward;
   }

   
   public void setDescribe(final String describe) {
      this.describe = describe;
   }

   
   public boolean equals(final Object o) {
      if (o == this) {
         return true;
      } else if (!(o instanceof Reward other)) {
         return false;
      } else {
          if (!other.canEqual(this)) {
            return false;
         } else {
            Object this$type = this.getType();
            Object other$type = other.getType();
            if (this$type == null) {
               if (other$type != null) {
                  return false;
               }
            } else if (!this$type.equals(other$type)) {
               return false;
            }

            Object this$items = this.getItems();
            Object other$items = other.getItems();
            if (this$items == null) {
               if (other$items != null) {
                  return false;
               }
            } else if (!this$items.equals(other$items)) {
               return false;
            }

            Object this$reward = this.getReward();
            Object other$reward = other.getReward();
            if (this$reward == null) {
               if (other$reward != null) {
                  return false;
               }
            } else if (!this$reward.equals(other$reward)) {
               return false;
            }

            Object this$describe = this.getDescribe();
            Object other$describe = other.getDescribe();
            if (this$describe == null) {
                return other$describe == null;
            } else return this$describe.equals(other$describe);
         }
      }
   }

   
   protected boolean canEqual(final Object other) {
      return other instanceof Reward;
   }

   
   public int hashCode() {
      int PRIME = 59;
      int result = 1;
      Object $type = this.getType();
      result = result * 59 + ($type == null ? 43 : $type.hashCode());
      Object $items = this.getItems();
      result = result * 59 + ($items == null ? 43 : $items.hashCode());
      Object $reward = this.getReward();
      result = result * 59 + ($reward == null ? 43 : $reward.hashCode());
      Object $describe = this.getDescribe();
      result = result * 59 + ($describe == null ? 43 : $describe.hashCode());
      return result;
   }

   
   public String toString() {
      Integer var10000 = this.getType();
      return "Reward(type=" + var10000 + ", items=" + this.getItems() + ", reward=" + this.getReward() + ", describe=" + this.getDescribe() + ")";
   }

   
   public Reward(final Integer type, final List<Item> items, final Item reward, final String describe) {
      this.type = type;
      this.items = items;
      this.reward = reward;
      this.describe = describe;
   }

   
   public Reward() {
   }

   public static class Item {
      private String tag;
      private Integer index;
      private Integer num;
      private Integer weight;

      
      public String getTag() {
         return this.tag;
      }

      
      public Integer getIndex() {
         return this.index;
      }

      
      public Integer getNum() {
         return this.num;
      }

      
      public Integer getWeight() {
         return this.weight;
      }

      
      public void setTag(final String tag) {
         this.tag = tag;
      }

      
      public void setIndex(final Integer index) {
         this.index = index;
      }

      
      public void setNum(final Integer num) {
         this.num = num;
      }

      
      public void setWeight(final Integer weight) {
         this.weight = weight;
      }

      
      public boolean equals(final Object o) {
         if (o == this) {
            return true;
         } else if (!(o instanceof Item other)) {
            return false;
         } else {
             if (!other.canEqual(this)) {
               return false;
            } else {
               Object this$index = this.getIndex();
               Object other$index = other.getIndex();
               if (this$index == null) {
                  if (other$index != null) {
                     return false;
                  }
               } else if (!this$index.equals(other$index)) {
                  return false;
               }

               Object this$num = this.getNum();
               Object other$num = other.getNum();
               if (this$num == null) {
                  if (other$num != null) {
                     return false;
                  }
               } else if (!this$num.equals(other$num)) {
                  return false;
               }

               Object this$weight = this.getWeight();
               Object other$weight = other.getWeight();
               if (this$weight == null) {
                  if (other$weight != null) {
                     return false;
                  }
               } else if (!this$weight.equals(other$weight)) {
                  return false;
               }

               Object this$tag = this.getTag();
               Object other$tag = other.getTag();
               if (this$tag == null) {
                   return other$tag == null;
               } else return this$tag.equals(other$tag);
            }
         }
      }

      
      protected boolean canEqual(final Object other) {
         return other instanceof Item;
      }

      
      public int hashCode() {
         int PRIME = 59;
         int result = 1;
         Object $index = this.getIndex();
         result = result * 59 + ($index == null ? 43 : $index.hashCode());
         Object $num = this.getNum();
         result = result * 59 + ($num == null ? 43 : $num.hashCode());
         Object $weight = this.getWeight();
         result = result * 59 + ($weight == null ? 43 : $weight.hashCode());
         Object $tag = this.getTag();
         result = result * 59 + ($tag == null ? 43 : $tag.hashCode());
         return result;
      }

      
      public String toString() {
         String var10000 = this.getTag();
         return "Reward.Item(tag=" + var10000 + ", index=" + this.getIndex() + ", num=" + this.getNum() + ", weight=" + this.getWeight() + ")";
      }

      
      public Item(final String tag, final Integer index, final Integer num, final Integer weight) {
         this.tag = tag;
         this.index = index;
         this.num = num;
         this.weight = weight;
      }

      
      public Item() {
      }
   }
}
